/**
 * File: StoreGraphController.java
 * Created by: mhaimel
 * Created on: Apr 21, 2010
 * CVS:  $Id: StoreGraphController.java 1.0 Apr 21, 2010 2:24:38 PM mhaimel Exp $
 */
package uk.ac.ebi.curtain.controller.graph;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import uk.ac.ebi.curtain.controller.SingleSubmitController;
import uk.ac.ebi.curtain.utils.CurtainUncheckedException;

/**
 * @author mhaimel
 *
 */
public class StoreGraphController extends SingleSubmitController<Serializable, Serializable> {

	@Override
	protected void singleSubmit(List<Serializable> inputList) {
		File gFile = getContext().currentFiles().getGraph(getGraphFilePrefix());
		getLog().info("Store Graph to file " + gFile + "...");
		try {
			getContext().getGraph().persist(gFile);
		} catch (IOException e) {
			throw new CurtainUncheckedException("Problems persisting Graph to " + gFile,e);
		}
		getLog().info("Finished storing Graph!");
		for(Serializable s : inputList){
			submit(s);
		}
	}
	
	private String getGraphFilePrefix() {
		String name = getQueueName().toLowerCase();
		int idx = name.indexOf(':');
		if(idx>0){
			name = name.substring(0, idx);
		}
		return name;
	}
	
}
